<?php

require_once dirname(__FILE__) . '/../lib/lrNewsletterAdminGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/lrNewsletterAdminGeneratorHelper.class.php';

/**
 * lrNewsletterAdmin actions.
 *
 * @package    LRCMS
 * @subpackage lrNewsletterAdmin
 * @author     Thomas Ohms <http://www.lokarabia.de>
 * @version    SVN: $Id: actions.class.php 26 2010-10-02 18:35:29Z tohms $
 */
class lrNewsletterAdminActions extends autoLrNewsletterAdminActions {

    public function executeSend(sfWebRequest $request) {
        $contentHtml = null;
        $contentText = null;

        $this->subscriberCount = 0;

        $this->articles = array();

        $newsletter = LrNewsletterTable::getInstance()->findOneById($request->getParameter('id'));

        $this->subject = $newsletter->subject;

        $subscribers = LrSubscriberTable::getInstance()->findBy('confirmed', true);
        $this->subscriberCount = $subscribers->count();
        $contactinfo='<div style="background:#ffffff; padding:2px"></div>
            <div style="background:#F2EBEF; padding:10px">
<p align="center">AVAILABLE ONLINE NOW </p>
                        <p align="center">&nbsp;</p>
                        <p align="center">LOG ON TO : <a href="http://www.onnobostro.com/">WWW.ONNOBOSTRO.COM</a></p>
                        <p align="center">Tel.: 9662120, 01711542638, email : <a href="mailto:onnobostro.com@gmail.com">onnobostro.com@gmail.com</a></p>

                        </div>';


//        $page = $newsletter->getPage();
        $contentHtml .= $this->renderArticle($newsletter->subject, $newsletter->summary);
        $contentText .= $this->renderArticle($newsletter->subject, $newsletter->summary, false);
        $contentHtml .= $this->addProductList($newsletter);
        $contentHtml .=         $contactinfo;
        $contentText .=         $contactinfo;
 
        $mail = $this->getService('mail')->setTemplate('newsletter');
        $recipients = array();

        foreach ($subscribers as $subscriber) {
            $mail->addValues(array(
                    'firstname' => $subscriber->firstname,
                    'lastname' => $subscriber->lastname,
                    'email' => $subscriber->email,
                    'content_text' => $contentText,
                    'content_html' => $contentHtml,
                    'unsubscribe_parameter' => '?remove=' . $subscriber->id,
                    'edit_parameter' => '?edit=' . $subscriber->id
            ));

            $recipients[$subscriber->email] = $subscriber->firstname . " " . $subscriber->lastname;
        }
        $this->contentHtml = $contentHtml;
        $this->contentText = $contentText;
        $mail = $mail->render();
        $mail->getMessage()->setTo($recipients);
        $mail->getMessage()->setSubject($newsletter->subject);

        $mail->send();

        $newsletter->set('sent_at', date("Y-m-d H:i"));
        $newsletter->save();

    }

    protected function renderArticle($title, $summary, $isHtml = true) {
        if ($isHtml) {
            $nl = "<br />";
            $title = '<div style="background:#3a275a; padding:10px">
<p align="center"><b><font size="5" color="dimgray"><font face="Tahoma"><font color="#ffffff">'.$title.'</font></b></p>
                      </div> <div style="height:5px; color:#dbd7d6">&nbsp;</div>' ;
            $content ='<div style="background:#0d8ea0; padding:10px"><p align="center"><font size="2" face="Tahoma" color="#ffffff">'.$summary.'</font></p></div>' ;
            $articleUrl =  '</p>';
        } else {
            $nl = "\r\n";
            $sep = '-------------------------------------------------';

            $title = "*== " . $title . "==*" . $nl;
            $content = $summary . $nl . $nl;
            $articleUrl =   $nl . $sep . $nl . $nl;
        }

        return $title . $content . $articleUrl;
    }
    protected function addProductList($newsletter) {

        $text = '<div style="background:#f2ebef; padding:10px;overflow:hidden"><br/>';
        $helper = $this->getHelper();
        foreach($newsletter->RelatedProductList as $product) {
            $component =  $product->getTable()->getComponentName();
            $page = DmPageTable::getInstance()->findOneByRecordIdAndModuleAndAction($product->id,$component,'show');
            $imgtext = '<img src="'.$helper->media($product->Image)->getAbsoluteSrc().'" width="175" height="230"/>';
            $productUrl = dm::getHelper()->link($page)->text($imgtext);
            $text.='<div style="float:left;border:5px solid #BDA3B1;margin:20px;text-align:center; padding:5px; display:block; width:200px; height:380px;">'.$productUrl.'<br/>'.$product->name.'<br/> Price :'.$product->price.'</div>';
        }
        $text .= '<br/><div style="clear:both"> </div>
            </div>';

        return $text;

    }

}
